package com.example.sherlockzhong.plush_v2.Subscriptions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.Models.WeatherModel;
import com.example.sherlockzhong.plush_v2.R;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

/*
 * Created by Doson on 2014/8/28.
 */

public class WeatherActivity extends Activity {
    HashMap<String, String> hashMap;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            WeatherActivity.this.stopReflashDialog();
            if (msg.what == 0x123) {
                if (hashMap != null)
                    initWeatherTable(WeatherActivity.this, hashMap, WeatherModel.getTime());
                else
                    Toast.makeText(WeatherActivity.this, "网络超时", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(WeatherActivity.this, "网络超时", Toast.LENGTH_SHORT).show();
            }
        }
    };
    private ProgressDialog pd;

    public static void initWeatherTable(Activity app, HashMap<String, String> hashmap, int index) {
        String weatherFullAddress = hashmap.get("Address");
        String weatherFullCondition = hashmap.get("Condition");
        String weatherFullWind = hashmap.get("WindPower");
        String weatherFullTemperature = hashmap.get("Temperature");
        String weatherTomorrowText = hashmap.get("TomorrowTemperature");
        String weatherDayAfterTomorrowText = hashmap.get("DayAfterTomorrowTemperature");
        String weatherThreeDaysFromToday = hashmap.get("ThreeDaysFromTodayTemperature");

        TextView weather_full_address = (TextView) app.findViewById(R.id.weather_full_address);
        weather_full_address.setText(weatherFullAddress);
        TextView weather_full_condition = (TextView) app.findViewById(R.id.weather_full_condition);
        weather_full_condition.setText(weatherFullCondition);
        TextView weather_full_temperature = (TextView) app.findViewById(R.id.weather_full_temperature);
        weather_full_temperature.setText(weatherFullTemperature);
        TextView weather_full_wind = (TextView) app.findViewById(R.id.weather_full_wind);
        weather_full_wind.setText(weatherFullWind);

        TextView weather_tomorrow_text = (TextView) app.findViewById(R.id.weather_tomorrow_text);
        weather_tomorrow_text.setText(weatherTomorrowText);
        TextView weather_day_after_tomorrow_text = (TextView) app.findViewById(R.id.weather_day_after_tomorrow_text);
        weather_day_after_tomorrow_text.setText(weatherDayAfterTomorrowText);
        TextView weather_three_days_from_today_text = (TextView) app.findViewById(R.id.weather_three_days_from_today_text);
        weather_three_days_from_today_text.setText(weatherThreeDaysFromToday);

        ((ImageView) app.findViewById(R.id.weather_full_icon)).setImageResource(Integer.parseInt(hashmap.get("TodayIcon")));
        ((ImageView) app.findViewById(R.id.weather_tomorrow_img)).setImageResource(Integer.valueOf(Integer.parseInt(hashmap.get("TomorrowIcon"))));
        ((ImageView) app.findViewById(R.id.weather_day_after_tomorrow_img)).setImageResource(Integer.valueOf(Integer.parseInt(hashmap.get("DayAfterTomorrowIcon"))));
        ((ImageView) app.findViewById(R.id.weather_three_days_from_today_img)).setImageResource(Integer.valueOf(Integer.parseInt(hashmap.get("ThreeDaysFromTodayIcon"))));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_full);
        ((BackButton) findViewById(R.id.weather_back)).setApp(this);
        Thread hit = new Thread() {
            @Override
            public void run() {
                hashMap = WeatherModel.getWeatherHashMap();
                handler.sendEmptyMessage(0x123);
            }
        };
        this.showReflashDialog("加载中...");
        hit.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    private void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(this);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(true);
        this.pd.setMessage(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */
    private void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }

    public void onDestory() {
        super.onDestroy();
        stopReflashDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            stopReflashDialog();
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
